package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.jmx.R;
import com.yiwei.jmx.adapter.MyRewardAdapter;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.InviteRewardOpenBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.Util;
import com.yiwei.jmx.view.RedRewardDialog;
import com.yiwei.jmx.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyRewardActivity extends BaseActivity implements NestedScrollView.OnScrollChangeListener {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.btn_withdraw)
    SuperButton btnWithdraw;
    @BindView(R.id.btn_invite)
    TextView btnInvite;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.btn_more)
    TextView btnMore;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.txt_invite_num)
    TextView txtInviteNum;
    private Unbinder unbiner;
    private MyRewardAdapter adapter;
    private String status;
    private int currentPage = 1;
    private boolean isOkRefresh = false;
    private RedRewardDialog openWardDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_reward);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("我的奖励");
        txtRight.setText("提现明细");
        openWardDialog = new RedRewardDialog(this);
        scrollView.setOnScrollChangeListener(this);
        txtRight.setVisibility(View.VISIBLE);

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getInviteRewardIndex();
            }
        });
        adapter = new MyRewardAdapter(this);
        adapter.setOnChooseListener(new MyRewardAdapter.OnChooseListener() {
            @Override
            public void onSuccess(String id) {
                getPrizeTake(id);
            }
        });
        list.setAdapter(adapter);
        getInviteRewardIndex();

    }

    public void getInviteRewardIndex() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);

        Api.api_service.getInviteRewardIndex().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        isOkRefresh = true;
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    txtInviteNum.setText("累计邀请人数(人)：" + result.data.invite_count);
                    txtMoney.setText(result.data.money_sum);
                    adapter.update(result.data.item);
                });
    }

    public void getPrizeTake(String id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);
        InviteRewardOpenBean bean = new InviteRewardOpenBean();
        bean.keyword = id;
        Api.api_service.getInviteRewardOpen(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    openWardDialog.setData(result.data.money);
                    openWardDialog.show();
                    getInviteRewardIndex();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.txt_right, R.id.btn_more, R.id.btn_back, R.id.btn_withdraw, R.id.btn_invite})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_right:
                Util.startActivity(this, WithdrawListActivity.class);
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_more:
                Util.startActivity(this, MyRewardMoreActivity.class);
                break;
            case R.id.btn_withdraw:
                Util.startActivity(this, WithdrawActivity.class);
                break;
            case R.id.btn_invite:
                Util.startActivity(this, MyQrCodeSecActivity.class);
                break;
        }
    }

    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int y, int oldScrollX, int oldScrollY) {
        if (y == (scrollView.getChildAt(0).getMeasuredHeight() - scrollView.getMeasuredHeight())) {
            //底部加载
            if (isOkRefresh) {
                isOkRefresh = false;
                currentPage++;
//                getPrizeList(currentPage);
            }
        }
    }
}
